<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Attestation_model extends CI_Model {
	function __construct(){
        parent::__construct();
    }

    function fstatus(){
    	return array(
    		0 => '等待审核',
    		1 => '审核通过',
    		2 => '审核不通过'
    	);
    }

    function add($data = array()){
    	$this->mycache->delete('attestation_user_'.$data['user_id']);
		$this->db->insert('zjy_attestation', $data);
		return $this->db->insert_id();
    }

    function update($data = array(),$where=array()){
    	if(empty($where)){
			return false;
		}
		$this->mycache->delete('attestation_user_'.$where['user_id']);
    	$this->db->where($where);
		$this->db->update('zjy_attestation', $data);
		return $this->db->affected_rows();
    }

    function get_one($where = array(),$select='*'){
		$where['is_del'] = 0;
    	$this->db->select($select);
		$this->db->where($where);
		$this->db->limit(1);
		$query = $this->db->get('zjy_attestation');
		return $query->row_array();
	}

	function user_all($user_id){
		$sql  ="select count(*) as num from zjy_attestation where is_del=0 and  user_id={$user_id}";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row['num'];
	}

	function get_list($where = array(),$select='*'){
		if(isset($where['user_id']) && $where['user_id'] > 0){
			$mkey = "attestation_user_".$where['user_id'];
			$ret = $this->mycache->get_array($mkey);
			if($ret != false){
				return $ret;
			}
		}
		$where['is_del'] = 0;
		$this->db->select($select);
		$this->db->where($where);
		$query = $this->db->get('zjy_attestation');
		$list = $query->result_array();
		if(isset($where['user_id']) && $where['user_id'] > 0){
			$mkey = "attestation_user_".$where['user_id'];
			$this->mycache->set_array($mkey,$list);
		}
		return $list;
	}

	function delete($where = array()){
		if(empty($where)){
			return false;
		}
		$this->mycache->delete('attestation_user_'.$where['user_id']);
		$this->db->where($where);
		$this->db->update('zjy_attestation', array('is_del'=>1));
		return $this->db->affected_rows();
	}

	function get_attestation_list($parms=array(),$per_page=0,$page_size=10){
		$tmp = array();
		foreach($parms as $key => $val){
			if($key == 'is_del'){
				$tmp[] = "a.{$key} = '{$val}'";
			}
			if($key == 'fstatus' && $val!=-1){
				$tmp[] = "a.{$key} = '{$val}'";
			}

			if($key == 'guarantee_id' && $val!=-1){
				$tmp[] = "a.{$key} = '{$val}'";
			}


			if($val){
				if($key =='username'){
					$tmp[] = "u.{$key} = '{$val}'";
				}elseif ($key =='kefu_userid'){
					$tmp[] = "v.{$key} = '{$val}'";
				}else{
					$tmp[] = "a.{$key} = '{$val}'";
				}
			}
		}

		$select = "a.*,u.username,u.realname";
		$where = '';
		$tmp && $where = " where " . implode(' and ', $tmp);
		$order = "order by id desc, fstatus asc";
		$limit = " limit {$per_page},{$page_size}";
		$sql = "select {$select} from zjy_attestation as a left join zjy_user as u on a.user_id = u.user_id left join zjy_user_vip as v on a.user_id = v.user_id {$where} {$order} {$limit}";
		$query = $this->db->query($sql);

		$data['list'] = $query->result_array();
		$sql = "select count(*) as count from zjy_attestation as a left join zjy_user as u on a.user_id = u.user_id left join zjy_user_vip as v on a.user_id = v.user_id {$where}";
		$query = $this->db->query($sql);
		$data['count'] = $query->row()->count;
		return $data;
	}
	
	//认证资料等待审核记录
	function attestation_count($where = null) {
		if(isset($where)) {
			$_where = ' where a.is_del = 0 AND a.guarantee_id = 0 ';
			$_where .= ' AND a.fstatus ='.$where;
		}
		$sql = "select count(id) as count from zjy_attestation as a left join zjy_user as u on a.user_id = u.user_id left join zjy_user_vip as v on a.user_id = v.user_id {$_where}";
		//echo $sql; exit;
		$query = $this->db->query($sql);
	 	return $query->row()->count;
	}

    function get_list_guarantee_by_id($att_id,$guarantee_id){
		$sql  ="select * from zjy_attestation where is_del=0 and  id in({$att_id})";
		$query = $this->db->query($sql);
		$list = $query->result_array();
		if($list){
			foreach ($list as $v) {
				$this->update(array('guarantee_id' =>$guarantee_id), array('id'=>$v['id']));
			}
			return 1;
		}
		return 0;

	}

	function get_list_guarantee($guarantee_id){
		$sql  ="select * from zjy_attestation where is_del=0 and  guarantee_id =({$guarantee_id})";
		$query = $this->db->query($sql);
		$list = $query->result_array();
		return $list;

	}







}